<?php

namespace app\admin\controller\subject;

// 引入基类控制器
use app\common\controller\Backend;

// 回收站
class Recyclebin extends Backend
{
  // 无需鉴权的方法,但需要登录
  protected $noNeedRight = ['index'];

  public function __construct()
  {
    parent::__construct();

    $this->SubjectModel = model('Subject.Subject');
    $this->OrderModel = model('Subject.Order');
  }

  protected $SubjectModel;
  protected $OrderModel;

  public function index()
  {
    return $this->view->fetch();
  }

  // 获取 subject 的回收站数据
  public function subject()
  {
    //设置过滤方法
    $this->request->filter(['strip_tags']);
    if ($this->request->isAjax()) {
      list($where, $sort, $order, $offset, $limit) = $this->buildparams();

      $total = $this->SubjectModel
        ->onlyTrashed()
        ->where($where)
        ->order($sort, $order)
        ->count();

      $list = $this->SubjectModel
        ->onlyTrashed()
        ->where($where)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->select();

      $result = array("total" => $total, "rows" => $list);

      return json($result);
    }

    return $this->view->fetch('index');
  }

  // subject 还原
  public function subjectRestore($ids = null)
  {
    if ($this->request->isAjax()) {
      if (!$ids) {
        $this->error('缺少参数');
        exit;
      }

      $list = $this->SubjectModel->onlyTrashed()->whereIn('id', $ids)->select();

      if (!$list) {
        $this->error(__('No Results were found'));
        exit;
      }

      $result = $this->SubjectModel->onlyTrashed()->whereIn('id', $ids)->update(['deletetime' => null]);

      if ($result) {
        $this->success('还原成功');
      } else {
        $this->error('还原失败');
      }
    }

    return $this->view->fetch('index');
  }

  // subject 销毁
  public function subjectDestroy($ids = null)
  {
    if ($this->request->isAjax()) {
      if (!$ids) {
        $this->error('缺少参数');
        exit;
      }

      // 导入附件模型
      $AttachmentModel = model('common/Attachment');

      $list = $this->SubjectModel->onlyTrashed()->whereIn('id', $ids)->select();

      if (!$list) {
        $this->error(__('No Results were found'));
        exit;
      }

      // 找到所有图片地址
      $thumbs = array_column($list, "thumbs");
      $thumbs = array_filter($thumbs);
      $thumbs = array_unique($thumbs);

      // 删除数据
      $result = $this->SubjectModel->onlyTrashed()->whereIn('id', $ids)->delete(true);

      if ($result) {
        if (empty($thumbs)) {
          foreach($thumbs as $v){
            // 拼接路径
            $path = '.' . $v;
            // 删除旧图片，如果图片删除成功，同时删除附件信息
            if (is_file($path) && @unlink($path)) {
              // 删除对应的附件信息
              $AttachmentModel->where(['url' => $v])->delete();
            }
          }
        }

        $this->success('销毁成功');
      } else {
        $this->error('销毁失败');
      }
    }

    return $this->view->fetch('index');
  }

  // 获取 order 的回收站数据
  public function order()
  {
    //设置过滤方法
    $this->request->filter(['strip_tags']);
    if ($this->request->isAjax()) {
      list($where, $sort, $order, $offset, $limit) = $this->buildparams();

      $total = $this->OrderModel
        ->onlyTrashed()
        ->where($where)
        ->order($sort, $order)
        ->count();

      $list = $this->OrderModel
        ->onlyTrashed()
        ->where($where)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->select();

      $result = array("total" => $total, "rows" => $list);

      return json($result);
    }

    return $this->view->fetch('index');
  }

  // order 还原
  public function orderRestore($ids = null)
  {
    if ($this->request->isAjax()) {
      if (!$ids) {
        $this->error('缺少参数');
        exit;
      }

      $list = $this->OrderModel->onlyTrashed()->whereIn('id', $ids)->select();

      if (!$list) {
        $this->error('无可恢复的数据');
        exit;
      }

      $data = [
        'id' => $ids,
        'deletetime' => null,
      ];

      $result = $this->OrderModel->onlyTrashed()->whereIn('id', $ids)->update(['deletetime' => null]);

      if ($result) {
        $this->success('还原成功');
      } else {
        $this->error('还原失败');
      }
    }

    return $this->view->fetch('index');
  }

  // order 销毁
  public function orderDestroy($ids = null)
  {
    if ($this->request->isAjax()) {
      if (!$ids) {
        $this->error('缺少参数');
        exit;
      }

      $list = $this->OrderModel->onlyTrashed()->whereIn('id', $ids)->select();

      if (!$list) {
        $this->error(__('No Results were found'));
        exit;
      }

      // 删除数据
      $result = $this->OrderModel->onlyTrashed()->whereIn('id', $ids)->delete(true);

      if ($result) {
        $this->success('销毁成功');
      } else {
        $this->error('销毁失败');
      }
    }

    return $this->view->fetch('index');
  }
}
